//
//  SystemInfoViewController.m
//  yigou
//
//  Created by 秦乾龙 on 17/1/16.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "SystemInfoViewController.h"
#import "Message.h"
@interface SystemInfoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,retain) UITableView *mainTableView;
@property (nonatomic,retain) NSMutableArray *msgArr;
@property (assign, nonatomic) BOOL pageToEnd;
@property (assign, nonatomic) NSInteger page;
@property (assign, nonatomic) NSInteger limit;
@end

@implementation SystemInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _msgArr = [NSMutableArray array];
    self.page = 1;
    self.limit = 5;
    [self setUI];
}
- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    
    [self request];
}
- (void) setUI
{
    [ViewFactory setNavigationBlack:self];
    self.navigationItem.titleView = [ViewFactory makeNavigationTitle:@"系统通知"];
    
    UIBarButtonItem *backBtn = [ViewFactory makeNavigationBackWhite:self action:@selector(goPop)];
    self.navigationItem.leftBarButtonItem = backBtn;
    _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,ScreenW , ScreenH-64) style:UITableViewStyleGrouped];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    [self.view addSubview:_mainTableView];
    [self addRefreshHeader:_mainTableView];
    [self addLoadMoreFooter:_mainTableView];
}
//下拉刷新
- (void)refresh {
    [super refresh];
    [self request];
}
//上拉加载
- (void)loadMore {
    if (self.pageToEnd) {
        [ViewFactory showInfoMsg:@"没有下一页了！"];
        [_mainTableView.footer endRefreshing];
        return;
    }
    
    DYBaseRequest *request = [DYBaseRequest new];
    NSString *url = [NSString stringWithFormat:@"%@/userCenter/getSysMessageList?page=%ld&limit=%ld", APIYIGOU, self.page,self.limit];
    DYLog(@".....%ld",self.page);
    request.requestUrl =  [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [ViewFactory showLoading];
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSDictionary *allDic = [NSDictionary changeType:request.responseJSONObject];
        NSArray *array = [[allDic objectForKey:@"data"] objectForKey:@"list"];
        DYLog(@".....%@",allDic);
        if (array.count == 0) {
            [ViewFactory showInfoMsg:@"没有下一页了！"];
            
        }
        else if (array.count<_limit)
        {
            self.pageToEnd = YES;
        }
        self.page += 1;
        DYLog(@".....%ld",array.count);
        [_msgArr addObjectsFromArray: array];
        DYLog(@".....%ld",_msgArr.count);
        [ViewFactory dismissLoading];
        [_mainTableView reloadData];
        [_mainTableView.footer endRefreshing];
    } error:^(YTKBaseRequest *request) {
        [_mainTableView.footer endRefreshing];
    } failure:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
        [_mainTableView.footer endRefreshing];
    }];
}

- (void)request
{
    self.page = 1;
    self.pageToEnd = NO;
    NSString *urlStr = [NSString stringWithFormat:@"%@/userCenter/getSysMessageList?page=%ld&limit=%ld",APIYIGOU,self.page,self.limit]
    ;
    
    DYBaseRequest *request = [DYBaseRequest new];
    request.requestUrl = urlStr;
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSDictionary *allDic = [NSDictionary changeType:request.responseJSONObject];
        NSLog(@"1111111%@",allDic);
//        _msgArr = [NSMutableArray array];
        [_msgArr removeAllObjects];
        [_msgArr addObjectsFromArray:allDic[@"data"][@"list"]];
        self.page+=1;
        [_mainTableView reloadData];
        [_mainTableView.header endRefreshing];
    } error:^(YTKBaseRequest *request) {
        [_mainTableView.header endRefreshing];
    } failure:^(YTKBaseRequest *request) {
        [_mainTableView.header endRefreshing];
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _msgArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
    {
        cell =[[NSBundle mainBundle ]loadNibNamed:@"ZYTableViewCell" owner:nil options:nil][0];
    }
    //设置cell点击背景色不变
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    CGFloat spaceToTop = 30*kScale;
    CGFloat spaceToLeft = 30*kScale;
    CGFloat lableH = 42*kScale;
    CGFloat redDotH = 20*kScale;
    Message *msg = [Message mj_objectWithKeyValues:_msgArr[indexPath.section]];
    UILabel *typeLable = [[UILabel alloc]init];
    if ([msg.msg_type isEqualToString:@"01-1"])
    {
        typeLable.text = @"订单";
    }
    else if ([msg.msg_type isEqualToString:@"01-2"])
    {
         typeLable.text = @"公告";
    }
    else if ([msg.msg_type isEqualToString:@"01-3"])
    {
         typeLable.text = @"通知";
    }
    else if ([msg.msg_type isEqualToString:@"02"])
    {
         typeLable.text = @"私信";
    }
    
    CGSize typeSize = [typeLable sizeThatFits:CGSizeMake(MAXFLOAT, lableH)];
    typeLable.frame = CGRectMake(spaceToLeft, spaceToTop, typeSize.width, typeSize.height);
    [cell addSubview:typeLable];
    UIView *redDotView = [[UIView alloc]init];
    redDotView.backgroundColor = ZallColorFromRGB(0xBE2828);
    redDotView.frame = CGRectMake(spaceToLeft+typeSize.width+10*kScale, spaceToTop, redDotH, redDotH);
    redDotView.layer.cornerRadius = redDotH/2.0;
    if ([msg.read_status isEqualToString:@"01"])
    {
        redDotView.hidden = YES;
    }
    [cell addSubview:redDotView];
    UILabel *timeLable = [[UILabel alloc]init];
    timeLable.text = msg.send_tm;
    CGSize timeSize = [timeLable sizeThatFits:CGSizeMake(MAXFLOAT, lableH)];
    timeLable.frame = CGRectMake(spaceToLeft+typeSize.width+20*kScale+20*kScale, spaceToTop, ScreenW, timeSize.height);
    timeLable.font = [UIFont systemFontOfSize:14];
    timeLable.textColor = ZallColorFromRGB(0x9e9e9e);
    [cell addSubview:timeLable];
    
    UILabel *contentLable = [[UILabel alloc]init];
    contentLable.text = msg.send_content;
//    contentLable.text = @"客户端叫哈看见很多事看见哈哈都会带回家啊点击活动哈就好大家好大家啊激动好激动好的";
    contentLable.numberOfLines = 0;
    contentLable.font = [UIFont systemFontOfSize:15];
    CGSize contentSize = [contentLable sizeThatFits:CGSizeMake(ScreenW-2*spaceToLeft, MAXFLOAT)];
    contentLable.frame = CGRectMake(spaceToLeft, spaceToTop+lableH+10*kScale, ScreenW-2*spaceToLeft, contentSize.height);
    contentLable.textColor = ZallColorFromRGB(0x000000);
    [cell addSubview:contentLable];
    cell.frame = CGRectMake(0, 0, ScreenW, contentLable.frame.origin.y+contentSize.height+spaceToTop);
    return cell;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSelector:@selector(cancleSelect) withObject:nil afterDelay:0.1f];
    
    
}
- (void)cancleSelect
{
    [_mainTableView deselectRowAtIndexPath:[_mainTableView indexPathForSelectedRow] animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //提前获取cell
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    
    //cell里面已经设置好了frame，这里就要返回cell的frame就可以了。
    return cell.frame.size.height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return 0.001;
    }
    return 20*kScale;
}
//修改状态栏字体
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (void)goPop
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
